package com.sczmgk.track.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;

import com.sczmgk.track.MyApp;
import com.sczmgk.track.R;
import com.sczmgk.track.activity.trackactivity.TracingActivity;
import com.sczmgk.track.base.CheckPermissionBaseActivity;
import com.sczmgk.track.constant.Constant;
import com.sczmgk.track.db.impl.UserDaoImpl;
import com.sczmgk.track.entity.User;
import com.sczmgk.track.entity.bean.UserData;
import com.sczmgk.track.httpUtils.MyHttp;
import com.sczmgk.track.test.MyCallback;
import com.sczmgk.track.utils.CacheUtils;
import com.sczmgk.track.utils.CommonUtils;
import com.sczmgk.track.utils.FileUtil;
import com.sczmgk.track.utils.MyLog;

import java.util.HashMap;
import java.util.Map;

import static com.sczmgk.track.MyApp.imei;


/**
 * Created by Administrator on 2017/5/11.
 */
public class SplashActivity extends CheckPermissionBaseActivity {

    private boolean isUpdata = false;
    private final String TAG = "SplashActivity";
    private ProgressDialog progressDialog;

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setTheme(R.style.SplashTheme);

        MyLog.e(TAG,"SplashActivity  onCreate");
        setContentView(R.layout.activity_splash);
        goneHeadView();
        checkStroage(0, TYPE_NORMAL);
    }

    private void onStroagePermissionOk() {
        checkPhone("");
    }

    private void initData() {
        MyLog.i(TAG, "phone state permission ok!");
        imei = CommonUtils.getPhoneImei(getApplicationContext());

        getUserInfo();
        startActivity();

    }

    /**
     * 获取用户信息
     */
    private void getUserInfo() {
        int loginUserid = CacheUtils.getLoginUserid(SplashActivity.this);
        if (loginUserid != 0){
            Map<String,Object> maps = new HashMap<>();
            maps.put("userid",loginUserid) ;
            maps.put("imei",imei) ;
            MyHttp.requestDataT(SplashActivity.this, Constant.GET_USER_INFO_URL, MyHttp.POST, null, maps, null, false, null, new MyCallback<UserData>() {

                @Override
                public void onSuccess(UserData userData) {
                    try {
                        parseJson(userData.data);
                    } catch (Exception e) {
                        e.printStackTrace();
                        MyLog.e(TAG,"Json解析错误");
                    }
                }

                @Override
                public void onUnknownError() {

                }

                @Override
                public void onError(String msg) {

                }

                @Override
                public void onSuccess() {

                }
            });
        }
    }


    private void parseJson(User user) {
        //设置登录状态
        MyApp.loginUserUid = user.userid;
        MyApp.loginUserName = user.username;
        CacheUtils.setLoginUserid(SplashActivity.this, user.userid);
//        CacheUtils.setJobNum(LoginActivity.this,user.worknumber);
        //存入数据库
        if (UserDaoImpl.isExists(SplashActivity.this, user.userid)) {
            User oldUser = UserDaoImpl.findByUid(SplashActivity.this, user.userid);
            user.setId(oldUser.getId());
            user.imageLocalPath = oldUser.imageLocalPath;
            UserDaoImpl.getIntance(SplashActivity.this).updata(user);
        } else {
            UserDaoImpl.getIntance(SplashActivity.this).insert(user);
        }
    }

    private void startActivity() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(isUpdata){
                    MyLog.d(TAG,"有更新，不跳转界面");
                }else{
                    MyLog.e(TAG,"没有更新进入登录页");
                    final boolean isLogin = CacheUtils.getLoginStatus(SplashActivity.this);
                    //TODO 判断是否登录以及轨迹是否结束
                    if (isLogin) {
                        startActivity(new Intent(SplashActivity.this, TracingActivity.class));
                    } else {
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    }
                    finish();
                }
            }
        }, 3000);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    public void onSelectedPhonePermission(String phoneNumber) {
        initData();
    }

    @Override
    public void onSelectedStroage(int position) {
        MyLog.i(TAG, "stroage permission ok!");
        onStroagePermissionOk();
        FileUtil.initAppStoragePath(getApplicationContext());

    }

    @Override
    public void onSelectCamera(int position) {

    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }

}
